﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.Role;
using Enterprise.Data.Role;
namespace Enterprise.Service.Role
{
    /// <summary>
    /// 角色应用服务
    /// </summary>
    public class RoleService
    {
        /// <summary>
        ///  角色缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Role";

        /// <summary>
        /// 角色列表 返回所有角色
        /// </summary>
        /// <returns></returns>
        public List<RoleEntity> RoleList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<RoleEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                RoleData data = new RoleData();
                List<RoleEntity> list = data.RoleList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 角色资料显示
        /// </summary>
        /// <param name="rId">角色编码</param>
        /// <returns></returns>
        public RoleEntity RoleDisp(int rId)
        {
            return RoleList().Where(p => p.ROLEID == rId).FirstOrDefault();
        }

        /// <summary>
        /// 角色操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void RoleExecute(RoleEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_ROLE(RNAME,RTYPE,RREMARK) values('" + entity.RNAME + "','" + entity.RTYPE + "','" + entity.RREMARK + "')";
                    break;
                case "Update":
                    cmd = "Update SYS_ROLE set RNAME='" + entity.RNAME + "' , RTYPE='" + entity.RTYPE + "' , RREMARK='" + entity.RREMARK + "'  where ROLEID=" + entity.ROLEID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_ROLE where ROLEID=" + entity.ROLEID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                RoleExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 组织机构操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void RoleExecuteBySql(string cmd)
        {
            RoleData data = new RoleData();
            data.RoleExecute(cmd);
            CacheHelper.RemoveCache(cacheKey);
        }

    }
}
